package org.ilovecode.display {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import org.ilovecode.events.ApartmentEvent;
	import org.ilovecode.Utilities;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class Floor extends MovieClip {
		
		//private var rooms:Array;
		private var roomsWithData:Array;
		private var floorID:int = 0;
		
		public function Floor() {
			//init();
		}
		
		public function init(floorID:int):void {
			this.floorID = floorID;
			roomsWithData = new Array();
			initListeners();
			loadFloorXML();
		}
		
		private function initListeners():void {
			
		}
		
		private function loadFloorXML():void {
			var url:String =  "http://www.centar-bundek.hr/xml/searchbyfloor/" + (floorID + 9);
			Utilities.loadURL(url, loadFloorXMLCompleted);
		}
		
		private function loadFloorXMLCompleted(event:Event):void {
			var xml:XML = XML(event.target.data);
			
			if (xml.children().length() > 0) {
				for each(var item:XML in xml.children()) {
					var id:String = item.id;
					var code:String = item.code;
					var status:String = item.state;
					var price:String = item.price;
					setRoomData(id, code, status, price);
				}
			} else {
				throw new Error("Error reading floors from database");
			}
			
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function setRoomData(id:String, code:String, status:String, price:String):void {
			var name:String = convertRoomCodeToName(code);
			var instance:DisplayObject = this.getChildByName(name);
			if (instance) {
				instance.addEventListener(MouseEvent.CLICK, roomClick);
				instance.addEventListener(MouseEvent.MOUSE_OVER, roomMouseOver);
				instance.addEventListener(MouseEvent.MOUSE_OUT, roomMouseOut);
				var room:FloorLayoutBaseRoom = new FloorLayoutBaseRoom(instance, id, code, status, price);
				room.alpha = 0;
				roomsWithData.push(room);
			}
		}
		
		private function roomMouseOver(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.MOUSE_OVER, event.currentTarget.name));
		}
		
		private function roomMouseOut(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.MOUSE_OUT, event.currentTarget.name));
		}
		
		private function roomClick(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.CLICK, event.currentTarget.name));
		}
		
		public function getRoomsList():Array {
			return roomsWithData;
		}
		
		public function getRoomByName(name:String):FloorLayoutBaseRoom {
			for each (var room:FloorLayoutBaseRoom in roomsWithData) {
				var roomName:String = convertRoomCodeToName(room.code);
				if (roomName == name) {
					return room;
				}
			}
			
			return null;
		}
		
		private function convertRoomCodeToName(code:String):String {
			var name:String = code.replace("-ST", "");
			if (name.charAt(name.length - 1) == ".") {
				name = name.slice(0, (name.length - 1));
			}
			name = name.replace(/\./gi, "_");
			return name;
		}
		
		/*private function configRooms():void {
			for (var i:uint = 0; i < this.numChildren; i++) {
				var child:DisplayObject = this.getChildAt(i);
				if (child is MovieClip) {
					rooms.push(child);
				}
			}
		}*/
		
	}

}